
 /**
 *
 * @param {Object|Number} obj
 * @param {Boolean|String} flag
 * @returns Enum
 */
export class Enum {
    constructor(obj, flag) {

        if (typeof obj == 'number') {
            if (typeof flag === 'string')
                return Object.assign(obj, { desc: flag });
            if (typeof flag === 'object')
                return Object.assign(obj, flag);
            return obj;
        }
        let keys = Object.keys(obj);

        this.valueOf = (value) => {
            if (typeof value == 'number') {
                let ks = this.getKeys(value);
                return ks.map(key => obj[key].desc || key).join('、');
            }
            console.warn("valueOf 必须是 number 类型");
            return '';
        };

        this.getValues = function (value) {
            var keys = this.getKeys(value);
            return keys.map(key => parseInt(obj[key]));
        };

        this.getKeys = function (value) {
            let ks = new Set();
            for (let index = 0; index < keys.length; index++) {
                const key = keys[index];
                if (obj[key] == value) {
                    ks.add(key);
                }
                if (flag && (obj[key] & value) == obj[key]) {
                    ks.add(key);
                }
            }
            return Array.from(ks);
        };

        this.options = function () {
            return keys.map(key => ({
                text: obj[key].desc,
                value: parseInt(obj[key])
            }));
        };


        this.keys = function () {
            return keys;
        };

        this.values = function () {
            return keys.map(key => parseInt(obj[key]));
        };
    }
}